<template>
  <el-form v-model="form" label-width="60px" label-position="left" size="mini">
    <el-form-item label="分栏:">
      <el-radio-group v-model="form.columnEnabled" @change="handleColumnEnabled">
        <el-radio-button :label="false">禁用</el-radio-button>
        <el-radio-button :label="true">启用</el-radio-button>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="栏数:">
      <el-select v-model="form.columnCount" :disabled="!form.columnEnabled" style="width: 120px;" @change="handleColumnCount">
        <el-option
          v-for="(item,index) in columnCountOptions"
          :key="index+1"
          :value="item.value"
          :label="item.label"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="栏间距:">
      <el-input v-model="form.columnMargin" :disabled="!form.columnEnabled" oninput="value=value.replace(/[^\d]/g,'')" style="width: 120px;" size="mini" @change="handleColumnMargin" />
    </el-form-item>
  </el-form>
</template>

<script>
import { pointToMM, mmToPoint } from '../../Utils.js'

export default {
  name: 'SubFieldSetting',
  props: {
    context: {
      type: Object,
      required: true
    }
  },
  data: function() {
    return {
      form: {
        columnEnabled: false,
        columnCount: 2,
        columnMargin: 2
      },
      columnCountOptions: [
        { label: '2栏', value: 2 },
        { label: '3栏', value: 3 },
        { label: '4栏', value: 4 },
        { label: '5栏', value: 5 },
        { label: '6栏', value: 6 },
        { label: '7栏', value: 7 },
        { label: '8栏', value: 8 },
        { label: '9栏', value: 9 },
        { label: '10栏', value: 10 }
      ]
    }
  },
  watch: {
    context: function(val) {
      this.init()
    }
  },
  mounted: function() {
    this.init()
  },
  methods: {
    init() {
      this.paper = this.context.reportDef.paper
      if (this.paper.columnEnabled) {
        this.columnEnabled = true
      }
      this.form.columnCount = this.paper.columnCount || 2
      if (this.form.columnMargin !== null || this.form.columnMargin !== undefined) {
        this.form.columnMargin = pointToMM(this.paper.columnMargin)
      }
    },
    handleColumnEnabled(val) {
      if (val) {
        this.paper.columnCount = this.form.columnCount
        this.paper.columnMargin = mmToPoint(this.form.columnMargin)
      }
      this.paper.columnEnabled = val
    },
    handleColumnCount(val) {
      this.paper.columnCount = val
    },
    handleColumnMargin(val) {
      this.paper.columnMargin = mmToPoint(val)
    }
  }
}
</script>

<style>
</style>
